package com.lkx.service.impl;


import com.lkx.dao.GoodsStatisticsMapper;
import com.lkx.domain.GoodsStatistics;
import com.lkx.service.GoodsStatisticsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * ClassName: YsGoodsStatisticsServiceImpl
 * Function:  TODO
 * Date:      2019-10-30 13:58:22
 * author     素剑步青尘
 * version    V1.0
 */
@Service
public class GoodsStatisticsServiceImpl implements GoodsStatisticsService {

	private static Logger log = LoggerFactory.getLogger(GoodsStatisticsServiceImpl.class);

	@Autowired
	private GoodsStatisticsMapper goodsStatisticsMapper;


	@Transactional
	@Override
	public void testMysqlLock(Long goodsId1,Long goodsId2) throws InterruptedException {
		log.info("开始事务1-1==={}",goodsId1);
		GoodsStatistics goodsStatistics = goodsStatisticsMapper.selectByPrimaryKey(goodsId1);
		goodsStatistics.setGoodsSales(goodsStatistics.getGoodsSales()+1);
		goodsStatisticsMapper.updateByPrimaryKey(goodsStatistics);

		Thread.sleep(1000);
		log.info("开始事务1-2==={}",goodsId2);
		goodsStatistics = goodsStatisticsMapper.selectByPrimaryKey(goodsId2);
		goodsStatistics.setGoodsSales(goodsStatistics.getGoodsSales()+1);
		goodsStatisticsMapper.updateByPrimaryKey(goodsStatistics);
	}
}

